package com.lightsh.gui;

import com.lightsh.motioner.ScreenTracker;
import com.lightsh.utility.Utility;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class TransferScreen extends BaseScreen{
	
	private static final String TAG = "TransferScreen";
	private static final boolean DEBUG = true;
	
	protected boolean mEnableTouchTransfer = true;
	
	private ScreenTracker mScreenTracker;
	
	private Rect mCurrentRect;
	private float mScreenScale;
	
	protected Rect mSrc;
	protected RectF mDst;

	public TransferScreen(Context context, int screenW, int screenH, int totalScreenW, int totalScreenH) {
		super(context, screenW, screenH);
		mScreenTracker = new ScreenTracker(mScreenW, mScreenH, totalScreenW, totalScreenH);
		mSrc = new Rect();
		mDst = new RectF();
		updateFromTracker();
	}
	
	
	private void updateFromTracker(){
		mCurrentRect = mScreenTracker.getDstRectClone();
		mScreenScale = mScreenTracker.getScreenScale();
	}
	
	public int getTotalScreenWidth(){
		return mScreenTracker.getTotalScreenWidth();
	}
	
	public int getTotalScreenHeight(){
		return mScreenTracker.getTotalScreenHeight();
	}
	
	public Rect getCurrentRectClone(){
		return new Rect(mCurrentRect);
	}

	/**
	 * 移动屏幕(调整显示世界中的哪一个范围的物体)
	 * @param stepX 屏幕向右或左(根据正负)移动的距离，以px为单位
	 * @param stepY 屏幕向下或上(根据正负)移动的距离，以px为单位
	 */
	public void moveScreenForward(int stepX, int stepY){
		mScreenTracker.moveScreenForward(stepX, stepY);
	}
	
	/**
	 * 将屏幕移动到世界的指定位置的
	 * @param xPosition		屏幕左边所处的坐标
	 * @param yPosition		屏幕上边所处的坐标
	 */
	public void moveScreenTo(int xPosition, int yPosition){
		mScreenTracker.checkAndMoveScreenTo(xPosition, yPosition);
	}

	@Override
	public float getScreenScale(){
		return mScreenScale;
	}

	/**
	 * 设置当前缩放比例
	 */
	public void setScreenScale(float scale){
		mScreenTracker.setScreenScale(scale);
	}
	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawColor(mBackgroundColor);
		// 画背景
		if(mBackground != null){
			// 需要根据mScreenRect位置绘制图片，如果图片不够世界的范围大，则需要需要平铺显示
			
			final float worldScale = getScreenScale();
			final Rect screenRect  = getCurrentRectClone();
			final int totalScreenW = screenRect.width();
			final int totalScreenH = screenRect.height();
			
			int needDrawW = totalScreenW;
			
			final int screenLeft =  screenRect.left;
			final int screenTop = screenRect.top;
			
			// 首先判断横向需要绘制几次
			while (needDrawW > 0) {
				int backgroundXPositon = (screenLeft + totalScreenW - needDrawW)  % mBackgroundW;
				int nextDrawWidth = Math.min(needDrawW, mBackgroundW - backgroundXPositon);

				int needDrawH = totalScreenH;
				while (needDrawH > 0) {
					int backgroundYPosition = (screenTop + totalScreenH - needDrawH ) % mBackgroundH;
					int nextDrawHeight = Math.min(needDrawH, mBackgroundH - backgroundYPosition);
					
					mSrc.left = backgroundXPositon;
					mSrc.top = backgroundYPosition;
					mSrc.right = backgroundXPositon + nextDrawWidth;
					mSrc.bottom = backgroundYPosition + nextDrawHeight;
					
					mDst.left = (totalScreenW - needDrawW) * worldScale;
					mDst.top = (totalScreenH - needDrawH) * worldScale;
					mDst.right = (totalScreenW - needDrawW + mSrc.width()) * worldScale;
					mDst.bottom = (totalScreenH - needDrawH + mSrc.height()) * worldScale;
					canvas.drawBitmap(mBackground, mSrc, mDst, mPaint);
					needDrawH -= nextDrawHeight;
					
				}

				needDrawW -= nextDrawWidth;
			}
		}
		
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}


	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		return false;
	}


	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if(super.onTouchEvent(ev)){
			return true;
		}
		return handleTouchEvent(ev);
	}

	private boolean handleTouchEvent(MotionEvent ev){
		// FIXME 何时判断是否需要屏幕滚动
		if(mEnableTouchTransfer){
			return mScreenTracker.onTouchEvent(ev);
		}
		return false;
	}

	@Override
	public void onUpdate() {		
		// 在这里将屏幕的转换应用到实际的绘制中，而不是根据用户动作实时更新，否者绘制时会产生残影(延迟)
		updateFromTracker();
	}
	
	public void enableTouchTransfer(boolean enable){
		mEnableTouchTransfer = enable;
	}
	
	private void logTip(String tip){
		if(DEBUG){
			Log.d(TAG, tip == null ? "" : tip);
		}
	}


	@Override
	public float getScreenPositionX() {
		return mCurrentRect.left;
	}


	@Override
	public float getScreenPositionY() {
		return mCurrentRect.top;
	}
	
}
